var room_id, game_id, room_name;
var chat = {
	//连接建立时发送登录信息
	onopen : function(){
	    var _opt = '{"type":"gamelist"}';
	    ws.send(_opt);
	},
	onchecklogin : function(){
		var _opt = getToKen();
		_opt['type'] = "checklogin";
		isDebug && console.log(JSON.stringify(_opt));
	    ws.send(JSON.stringify(_opt));
	},
	onmessage : function(e){
		var data = eval("("+e.data+")");
		isDebug && console.log("SOCKET返回："+JSON.stringify(data));
	    switch(data['type']){
	        case 'gamelist':
	            plus.storage.setItem('gamesList',JSON.stringify(data.gameslist));
	            initGames(data);
	            break;
	        case 'checklogin':
	        	switch(data['state']){
	        		case 200:
	        			//开启游戏页面
						mui.openWindow({  
						    url:"../room/room-redpacket.html",  
						    id:"room/room-redpacket.html",   
						    extras:{  
							    room_id : room_id,
							    game_id : game_id,
								room_name : room_name,
								username : plus.storage.getItem("username"),
						    },
						    show:{  
						      autoShow:false  
						    },
						    createNew:false,//是否重复创建同样id的webview，默认为false:不重复创建，直接显示   
						    waiting:{  
						      autoShow:true,//自动显示等待框，默认为true  
						      title:'正在加载',//等待对话框上显示的提示内容  
						    }  
						});
				        break;
				    case 301:
						openLoginWindow(data['room_id']);
				    	break;
				}
	    }
	},
};
mui.init();
mui.plusReady(function(e){
	//自动轮播
	mui("#slider").slider({
		interval : 5000,
	});
	console.log("userid="+plus.storage.getItem("user"));
	room_connect(chat.onopen,chat.onmessage);
	mui(".mui-scroll-wrapper").scroll();
	bindtap();
});

function bindtap(){
	window.addEventListener('loginSuccess',function(e){
		isDebug && console.log('home.js收到loginSuccess');
		isDebug && console.log(JSON.stringify(e));
	},false);
	window.addEventListener('updateData',function(e){
		var data = e.detail;
		switch(data['type']){
			case 'online':
				document.querySelector("#onlineNumbers").innerText = data.online;
				break;
			case 'init':
				break;
		}
	},false);
	mui("#game-list").on("tap","a",function(e){
		room_id = this.getAttribute("room_id");
		game_id = this.getAttribute("game_id");
		room_name = this.getAttribute("room_name");
		var _token = getToKen();
		isDebug && console.log(room_id+"检测用户登录状态："+_token['token']);
		if(_token['token']==''){
			isDebug && console.log('没有登录');
	    	openLoginWindow(room_id);
	    	return false;
	   	}
		room_connect(chat.onchecklogin, chat.onmessage,false); 
	});
}

var initGames = function(data){
	document.querySelector("#onlineNumbers").innerText = data['online'];
	//更新红包游戏列表
	var gameslist = plus.storage.getItem('gamesList');
	var slider = document.getElementById("slider")
	slider.innerHTML = template('bannerTemplate', data);
	isDebug && console.log(gameslist);
	gameslist = JSON.parse(gameslist);
	if(gameslist){
		//列出可玩的红包游戏
        var games_ul = document.getElementById("game-list");
        games_ul.innerHTML = '';
        mui.each(gameslist,function(index,v){
        	var li = document.createElement("li");
        	li.className = "mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-4";
			li.innerHTML = '<a href="#" room_id="game_'+v.id+'" game_id="'+v['id']+'" room_name="'+v.title+'">\
							<span class="mui-icon"><img src="'+v.icon+'" /></span>\
							<div class="mui-media-body">'+v.title+'</div>\
							</a>';
			games_ul.appendChild(li);
        });
        games_ul.innerHTML += '<li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-4">\
									<a href="#" room_id="0" class="mui-disabled">\
										<span class="mui-icon"><img src="../imgs/icon-more.png" /></span>\
										<div class="mui-media-body">更多</div>\
									</a>\
								</li>';
	}
}
